﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BankApp.Models;
using IBS.Math;


namespace BankApp.Controllers
{
    public class DepositCalculatorController : Controller
    {
        //
        // GET: /DepositCalculator/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CalculateFV() { return Show(CalcType.FV); }
        public ActionResult CalculatePV() { return Show(CalcType.PV); }
        public ActionResult CalculatePMT() { return Show(CalcType.Payment); }
        public ActionResult CalculateYRS() { return Show(CalcType.Period); }
        public ActionResult CalculateRate() { return Show(CalcType.Rate); }

        public ActionResult Show(CalcType calcType)
        {
            return View("Calculator", new CalcModel { CalcType = (CalcType)calcType });
        }

        [HttpPost]
        public ActionResult Calculate(CalcModel model)
        {
            if (model.CalcType == CalcType.Rate)
            {
                try
                {
                    model.Solution = TVM.rate(model.Period, model.Payment, model.PV, model.FV);
                }
                catch
                {
                    ModelState.AddModelError("", "please, ensure that the input make sence");
                }
            }
            else
            {
                try
                {
                    switch (model.CalcType)
                    {
                        case CalcType.PV:
                            model.Solution = model.ContCompounding
                                ? TVM.pvCont(model.Rate, model.Period, model.FV)
                                : TVM.pv(model.Rate, model.Period, model.Payment, model.FV, model.Frequency);
                            break;
                        case CalcType.FV:
                            model.Solution = model.ContCompounding
                                ? TVM.fvCont(model.Rate, model.Period, model.PV)
                                : TVM.fv(model.Rate, model.Period, model.Payment, model.PV, model.Frequency);
                            break;
                        case CalcType.Payment:
                            model.Solution = TVM.pmt(model.Rate, model.Period, model.PV, model.FV, model.Frequency);
                            break;
                        case CalcType.Period:
                            model.Solution = TVM.nper(model.Rate, model.Payment, model.PV, model.FV);
                            if (model.Solution <= 0)
                            {
                                ModelState.AddModelError("", "please, ensure that the input make sence");
                            }
                            break;
                        case CalcType.Rate:
                            model.Solution = TVM.rate(model.Period, model.Payment, model.PV, model.FV);
                            break;
                        default:
                            ModelState.AddModelError("", "invalid calculation type");
                            break;
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            return View("Calculator", model);
        }
    }
}
